package com.visionblue.business.ship.model.repository.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

/**
 * @author: xlh
 * @date: 2020-05-21 10:26 AM
 * @desc:
 */
@Database(
    entities = [CollectionRecord::class, CachedOrder::class, MeasureMenuItem::class],
    version = 3,
    exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {

    abstract fun collectionRecordDao(): CollectionRecordDao

    abstract fun recordAndOrderDao(): RecordAndOrderDao

    abstract fun cachedOrderDao(): CachedOrderDao

    abstract fun measureMenuItemsDao(): MeasureMenuItemDao

    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getDatabase(context: Context): AppDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }

            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "app_db"
                ).build()
                INSTANCE = instance
                return instance
            }
        }
    }
}